package lium.lbs.lbsim.activity

import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.Gravity
import android.view.View
import android.widget.Toast
import com.buz.hjcdriver.bean.AResultBean
import com.lmlibrary.Constants
import com.lmlibrary.bean.ResponseBean
import com.lzy.okgo.model.Response
import kotlinx.android.synthetic.main.activity_frienddetail.*
import lium.lbs.lbsim.R
import lium.lbs.lbsim.bean.FriendInfoBean
import lium.lbs.lbsim.jgchat.activity.ChatActivity
import lium.lbs.lbsim.jgchat.application.GJApp
import lium.lbs.lbsim.jgchat.utils.photovideo.takevideo.utils.LogUtils
import lium.lbs.lbsim.listener.ReMarketMessageEven
import lium.lbs.lbsim.listener.SystemMessageEven
import lium.lbs.lbsim.utils.IAlertDialog
import lium.lbs.lbsim.utils.SpUtils
import lium.lbs.lbsim.utils.ToastUtils
import lium.lbs.lbsim.utils.callbck.DialogCallback
import lium.lbs.lbsim.zfw.SetRemarkActivity
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class FriendDetailActivity : BaseActivity() {

    var fid: String? = null
    internal var dialog: IAlertDialog? = null
    var id: String? = null

    override fun getLayoutId(): Int {
        if (!EventBus.getDefault().isRegistered(this))
        {
            EventBus.getDefault().register(this);
        }
        return R.layout.activity_frienddetail
    }

    override fun initView(savedInstanceState: Bundle?) {
        setTitleWithBack("")
        addOnClickListeners(R.id.to_change, R.id.tochat, R.id.contacts_detel, R.id.contacts_add)
        fid = intent.getStringExtra("fid")
    }


    fun getDataList() {
        val param = HashMap<String, String>()
        param["mid"] = SpUtils.getInstance().get(Constants.memberId, "") as String
        param["fid"] = fid.toString()//1：找人；2：找群
        postData(
            "/frinds/frindInfo",
            param,
            object : DialogCallback<ResponseBean<FriendInfoBean>>(this) {
                override fun onSuccess(response: Response<ResponseBean<FriendInfoBean>>) {
                    setDataList(response.body().data)
                }
            })
    }

    fun setDataList(list: FriendInfoBean) {
        if (list?.friend?.isFriend.toString().equals("1")) {//1,是朋友 0，不是朋友
            isfriend.visibility = View.VISIBLE
            nofriend.visibility = View.GONE

        } else {
            isfriend.visibility = View.GONE
            nofriend.visibility = View.VISIBLE
        }
        displayImage(Constants.Host_Pic + list?.friend.headImage, R.drawable.icon_defalut_head, contacts_head)
        contacts_name.text = list?.friend?.uniqueCode
        contacts_nc.text = if (TextUtils.isEmpty(list?.friend?.remark)) {
            list?.friend?.nickName
        } else {
            list?.friend?.remark
        }
        contacts_dec.text = list?.friend?.signature
        contacts_where.text = list?.friend?.province + list?.friend?.city
        id = list?.friend?.id;


    }

    override fun onClick(v: View?) {
        super.onClick(v)
        when (v!!.id) {
            R.id.to_change -> {
                val intent = Intent(this, SetRemarkActivity::class.java)
                intent.putExtra("fid", fid)
                startActivityForResult(intent, 150)
            }

            R.id.tochat -> {
                val intent = Intent(this, ChatActivity::class.java)

                intent.putExtra(GJApp.CONV_TITLE, contacts_nc.text)
                intent.putExtra(GJApp.TARGET_ID, id)
                intent.putExtra(GJApp.TARGET_APP_KEY, Constants.TARGET_APP_KEY)
                LogUtils.e(
                    "--mUserInfo.getNotename--" + title + "--GJApp.TARGET_ID--" + SpUtils.getInstance().get(Constants.nickName,
                        ""
                    ) as String + "--GJApp.TARGET_APP_KEY--" + Constants.TARGET_APP_KEY
                )

                startActivityForResult(intent, 100)

            }
            R.id.contacts_detel -> {
                sureDetel("你确认要删除你的您的好友？")

            }
            R.id.contacts_add -> {
                addFriend()

            }
        }
    }

    fun sureDetel(msg: String) {
        dialog = IAlertDialog(this, IAlertDialog.LayoutStyle.READALL, Gravity.CENTER)
        dialog!!.setMessage(msg)
        dialog!!.setPositiveMsg("确定")
        dialog!!.setPositiveOnClickListener { dialog, which ->
            detelFriend()
        }
        dialog!!.setNegativeMsg("取消")
        dialog!!.setPositiveOnClickListener { dialog, which ->
            finish()
        }
        dialog!!.show()
    }

    fun detelFriend() {
        val param = HashMap<String, String>()
        param["mid"] = SpUtils.getInstance().get(Constants.memberId, "") as String
        param["fid"] = fid.toString()//1：找人；2：找群
        postData(
            "/frinds/frienddelete",
            param,
            object : DialogCallback<ResponseBean<AResultBean>>(this) {
                override fun onSuccess(response: Response<ResponseBean<AResultBean>>) {
                    setDatadetelFriend(response.body().msg)

                }
            })
    }

    fun setDatadetelFriend(msg: String) {
        ToastUtils.showLongToast(msg)
        finish()
    }

    fun addFriend() {
        val param = HashMap<String, String>()
        param["mid"] = SpUtils.getInstance().get(Constants.memberId, "") as String
        param["fid"] = fid.toString()
        param["applyInfo"] = SpUtils.getInstance().get(Constants.nickName, "") as String + "请求添加好友"
        postData(
            "/frinds/frindadd",
            param,
            object : DialogCallback<ResponseBean<AResultBean>>(this) {
                override fun onSuccess(response: Response<ResponseBean<AResultBean>>) {
                    setAddFriend(response.body().msg)
                }
            })
    }

    fun setAddFriend(msg: String) {
        ToastUtils.showToast(msg)
        finish()
    }


    override fun getNetWorkData() {
        getDataList()
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    fun Event(reMarketMessageEven: ReMarketMessageEven) {
        when (reMarketMessageEven.message) {
            1 -> {
                EventBus.getDefault().post(ReMarketMessageEven(2))
                finish()
            }
        }
    }

}